package com.yycx.module.bus.provider.handler.settle;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 未结算列表
 *
 * @author zyf
 */
@Component("noSettlePageListHandler")
public class NoSettlePageListHandler implements PageInterceptor<BusOrderDetails> {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BaseUserDeptMapper userDeptMapper;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        Long deptId = params.getLong("deptId");
        String refundDate1 = params.get("refundDate1");
        String refundDate2 = params.get("refundDate2");
        Integer auth = cq.getInt("auth", 0);
        OpenUser openUser = OpenHelper.getUser();
        Long companyId=openUser.getCompanyId();
        cq.addSelect(BusOrder.class, "orderId", "orderNo", "sendDate", "supplierId", "customBondType");
        cq.addSelect(BusOrderDetails.class, "orderDetailsId", "deptId", "parentId", "salePrice", "saleNum", "saleAmount", "settleAmount", "settleAmount2","settleAmount3", "supplierRetable", "supplierRetDate", "returnDate", "returnAmount", "kaipiaoId", "jiesuanId", "jiesuanId2", "jiesuanId3","settleRealAmount", "settleRealAmount2","settleRealAmount3","tbzjId","customBond","returnCustomBond");
        cq.addSelect(BusSupplier.class, "supplierName");
        cq.addSelect(BusCustom.class, "customName", "settleType");
        cq.addSelect(BusDrugs.class, "drugsName", "drugsSpec");
        cq.addSelect(BusDept.class, "deptName");
        cq.eq("ord.orderState", CommonConstants.INT_2);
        //2代表已结算完成
        cq.and(e -> e.nested(i -> i.isNull("jiesuanId").ne("settleAmount", 0)).or().nested(j -> j.isNull("jiesuanId2").ne("settleAmount2", 0)).or().nested(j -> j.isNull("jiesuanId3").ne("settleAmount3", 0)));
        if(companyId.equals(1L)){
            //厂家返款或者自营
            cq.and(e -> e.eq("supplierRefundState", 2).or().eq("drugs.ownerState", 1));
        }else{
            //厂家返款
            cq.eq("supplierRefundState", 2);
        }

        //已回款或者允许结算
        cq.and(e -> e.eq("custom.allowJs", "1").or().eq("bod.customReturnState", 2));
        cq.like(BusOrder.class, "orderNo");
        cq.like(BusCustom.class, "customName");
        cq.like(BusSupplier.class, "supplierName");
        cq.and(e -> e.eq("settleState", 0).or().eq("settleState2", 0).or().eq("settleState3", 0));
        if (auth.equals(CommonConstants.INT_1)) {
            Long userId = openUser.getUserId();
            List<Long> deptIds = userDeptMapper.selectUserDeptIds(userId);
            if (FlymeUtils.isNotEmpty(deptIds) && FlymeUtils.isEmpty(deptId)) {
                cq.in("bod.deptId", deptIds);
            } else {
                cq.eq("bod.deptId", deptId);
            }
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusOrder.class, "sendDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusOrder.class, "sendDate", endDate);
        }
        if (FlymeUtils.isNotEmpty(refundDate1)) {
            cq.ge(BusOrderDetails.class, "supplierRetDate", refundDate1);
        }
        if (FlymeUtils.isNotEmpty(refundDate2)) {
            cq.le(BusOrderDetails.class, "supplierRetDate", refundDate2);
        }
        cq.eq(true, "bod.companyId", companyId);
        cq.eq(BusOrderDetails.class, "deptId");
        cq.eq(BusOrderDetails.class, "supplierRefundState");
        cq.createJoin(BusDept.class);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusOrderDetails> cq, List<EntityMap> result, EntityMap extra) {

        for (EntityMap entityMap : result) {

            BigDecimal settleAmount = entityMap.getBigDecimal("settleAmount");
            BigDecimal settleAmount2 = entityMap.getBigDecimal("settleAmount2");
            BigDecimal settleAmount3 = entityMap.getBigDecimal("settleAmount3");

            Long jiesuanId=entityMap.getLong("jiesuanId");
            Long jiesuanId2=entityMap.getLong("jiesuanId2");
            Long jiesuanId3=entityMap.getLong("jiesuanId3");

            Long tbzjId = entityMap.getLong("tbzjId");
            BigDecimal customBond = entityMap.getBigDecimal("customBond");
            BigDecimal noReturnCustomBond=new BigDecimal("0");
            if (FlymeUtils.gtzero(customBond)) {
                 BigDecimal returnCustomBond=entityMap.getBigDecimal("returnCustomBond");
                 if(FlymeUtils.isEmpty(tbzjId)){
                     noReturnCustomBond=customBond.subtract(returnCustomBond);
                 }
            }

            BigDecimal noSettleAmount =settleAmount;
            BigDecimal noSettleAmount2 = settleAmount2;
            BigDecimal noSettleAmount3 = settleAmount3;

            if(FlymeUtils.isNotEmpty(jiesuanId)){
                BusPayment payment=paymentService.getById(jiesuanId);
                noSettleAmount=noSettleAmount.subtract(payment.getAmount());
            }
            if(FlymeUtils.isNotEmpty(jiesuanId2)){
                BusPayment payment=paymentService.getById(jiesuanId2);
                if(FlymeUtils.isNotEmpty(payment)) {
                    noSettleAmount2 =new BigDecimal("0");
                }
            }
            if(FlymeUtils.isNotEmpty(jiesuanId3)){
                BusPayment payment=paymentService.getById(jiesuanId3);
                if(FlymeUtils.isNotEmpty(payment)){
                    noSettleAmount3=noSettleAmount3.subtract(payment.getAmount());
                }

            }
            entityMap.put("noSettleAmount", noSettleAmount);
            entityMap.put("noSettleAmount2", noSettleAmount2);
            entityMap.put("noSettleAmount3", noSettleAmount3);
            entityMap.put("noReturnCustomBond", noReturnCustomBond);
        }
        return result;
    }
}
